package flexlingo.managers
{
	import flash.events.Event;
	import flash.utils.getQualifiedClassName;
	
	import mx.binding.utils.BindingUtils;
	import mx.collections.ArrayCollection;
	import mx.containers.*;

	import mx.containers.accordionClasses.AccordionHeader;
	import mx.controls.*;

	import mx.controls.dataGridClasses.DataGridColumn;
	import mx.core.Container;
	import mx.events.FlexEvent;
	import mx.managers.ISystemManager;
	import mx.utils.ObjectUtil;
	
	
	import flexlingo.utils.LingoUtils;
	import flexlingo.controls.PhraseItem;
	import flexlingo.binders.*;
	import flash.utils.getQualifiedSuperclassName;
	

	[Mixin]
	public class LingoManager
	{
		
		private static var systemManager:ISystemManager;
		
		public static function init( systemManager : ISystemManager ):void
		{	
			
			// listen for add to stage event
			systemManager.addEventListener( Event.ADDED_TO_STAGE,LingoManager.addBindingHandler,true);
			//systemManager.addEventListener( Event.REMOVED_FROM_STAGE,LingoManager.removeBindingHandler,true);
			// listen for creation complete events
			//systemManager.addEventListener(FlexEvent.CREATION_COMPLETE,LingoManager.bindingHandler,true);	
			
			
		}
		
		
		
		
		[Bindable]
		public static var phrases : ArrayCollection = new ArrayCollection();
		
		public static function bindProperty(phrase:String,site:Object,prop:String):void
		{				
			var key : String = LingoManager.getKey(phrase);

			var item : PhraseItem ;
			var index : int = LingoUtils.getIndexByID(LingoManager.phrases.source,"key",key);
			
			if( index == -1 )
			{		

				item = new PhraseItem(key,phrase)
				LingoManager.phrases.addItem(item);	
				
				BindingUtils.bindProperty(site,prop,item,"phrase");		
			
			}
			else
			{
				item = LingoManager.phrases[index] as PhraseItem;
				
				BindingUtils.bindProperty(site,prop,item,"phrase");
			}
			
			

		} 

		
		
		public static function translate(key:String,phrase:String):void
		{
			
			var item : PhraseItem = new PhraseItem(key,phrase);
			
			
			var index : int = LingoUtils.getIndexByID(LingoManager.phrases.source,"key",key);
			
			
			//trace(key + ":" + phrase + ":" + index);
			
			if( index  == -1 )
			{
				LingoManager.phrases.addItem( item );
			}
			else
			{
				(LingoManager.phrases[index] as PhraseItem).phrase = phrase;
			}
		}

		
		public static function getKey(phrase:String):String
		{	
			
			var s : String = phrase.toUpperCase();
			s = LingoUtils.trim(s);
			s = LingoUtils.replaceAll(s," ","_");
			s = LingoUtils.replaceAll(s,"'","");
		
			return s;
		}
		
		
		public static function removeBindingHandler(event:Event):void
		{	
			
			trace("removeBindingHandler");
				//setBinding(event.target);
				//BindingUtils. .bindProperty(site,prop,item,"phrase");
				
		}
		
		
		public static function addBindingHandler(event:Event):void
		{
				setBinding(event.target);
		}
		
		public static function setBinding(object:Object):void
		{
			var className : String = flash.utils.getQualifiedClassName(object);
			var superClassName : String = flash.utils.getQualifiedSuperclassName(object);
			//trace("className=" + className + " superClassname=" + superClassName);
			
			//trace(className);
			
			switch(className)
			{
				// controls
					
				case "mx.controls::Button": 
					ButtonBinder.bind(object as Button); 
				break;
				
				case "mx.controls::ComboBox": 
					ComboBoxBinder.bind(object as ComboBox); 
				break;
				
				case "mx.controls::DataGrid": 
					DataGridBinder.bind( object as DataGrid ); 
				break;
				
				

				case "mx.controls::Label": 
					LabelBinder.bind( object as Label ); 
				 break;
				
				case "mx.controls::LinkButton": 
					LinkButtonBinder.bind( object as LinkButton ); 
				break;


				case "mx.containers::Accordion": 
					AccordionBinder.bind(object as Accordion); 
				break;
				
				case "mx.containers.accordionClasses::AccordionHeader":
					AccordionBinder.bindAccordionHeader(object as AccordionHeader);
				break;
				
				case "mx.containers::FormItem": 
					FormItemBinder.bind(object as FormItem); 
				break;
				
				case "mx.containers::TabNavigator": 
					TabNavigatorBinder.bind(object as TabNavigator); 
				break;
				
				
				//containers
				case "mx.containers::Canvas": 
				case "mx.containers::HBox":
				case "mx.containers::VBox": 
				//case "mx.containers::FormItem": 
					ContainerBinder.bind( object as Container);
				break;
				
				
				case "mx.containers::Panel":
				case "mx.containers::TitleWindow": 
					PanelBinder.bind( object as Panel); 
				break;
	
				
			}

			
		}
		

		
				
	}
}